home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / OCI72 / NONBLK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-02  |  6.4 KB  |  278 lines

  1. /* Copyright (c) 1995 by Oracle Corporation */
  2. /*
  3.    NAME
  4.      nonblk.c
  5.    DESCRIPTION
  6.      Tests non-blocking connections
  7.    MODIFIED   (MM/DD/YY)
  8.     xxxxxxxx   03/15/95 -  change non-blocking printf to one printf
  9.     xxxxxxxx   03/08/95 -  change stderr to stdout
  10.     xxxxxxxx   03/05/95 -  Branch_for_patch
  11.     xxxxxxxx   03/01/95 -  Creation
  12. */
  13.  
  14. /*
  15.  * Name:        nonblk.c
  16.  *
  17.  * Description: This program performs a "long" running hardcoded insert 
  18.  *              and demonstrates the use of non-blocking calls.
  19.  *
  20.  * Changes:     modified login to non-blocking 
  21.  *
  22.  * Setup:       Run nonblk.sql before running this program.
  23.  *              Link program and run it. Program requires no arguments
  24.  *
  25.  * OCI Calls used:
  26.  *
  27.  *      Phase         OCI Call           Notes
  28.  *      ------------------------------------------------------------------
  29.  *      Login     -   olog               use nonblocking argument       
  30.  *      Open      -   oopen
  31.  *      Parse     -   oparse
  32.  *      Bind      -   obndra       
  33.  *      Describe  -   none               Hard-coded query
  34.  *      Define    -   none               insert statement
  35.  *      Execute   -   oexec
  36.  *      Fetch     -   none               insert
  37.  *      Close     -   oclose
  38.  *      Logoff    -   olof
  39.  *      Nonblocking - onbtst, onbclr
  40.  * 
  41.  * This program is for educational purposes.
  42.  *      
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <ctype.h>
  48. #include <string.h>
  49.  
  50. #include <oratypes.h>
  51. /* LDA and CDA struct declarations */
  52. #include <ocidfn.h>
  53. #ifdef __STDC__
  54. #include <ociapr.h>
  55. #else
  56. #include <ocikpr.h>
  57. #endif
  58. /* demo constants and structs */
  59. #include <ocidem.h>
  60.  
  61.  
  62. /* oparse flags */
  63. #define  DEFER_PARSE        1
  64. #define  NATIVE             1
  65. #define  VERSION_7          2
  66.  
  67. /* exit flags */
  68. #define OCI_EXIT_FAILURE 1
  69. #define OCI_EXIT_SUCCESS 0
  70.  
  71. #define BLOCKED -3123
  72. #define SUCCESS 0
  73.  
  74. Lda_Def lda;                                                   /* login area */
  75. ub1     hda[HDA_SIZE];                                          /* host area */
  76. Cda_Def cda;                                                  /* cursor area */
  77.  
  78. /* Function prototypes */
  79. void logon ();
  80. void logoff ();
  81. void setup();
  82. void err_report();
  83. void insert_data();
  84. void do_exit();
  85.  
  86. /* SQL statement used in this program */
  87.  
  88. text *sqlstmt = (text *)"INSERT INTO nonblktab (col1)\
  89.                          SELECT a.col1 \
  90.                          FROM nonblktab a, nonblktab b";
  91.  
  92. void main(argc, argv)
  93. eword argc;
  94. text **argv;
  95. {
  96.  
  97.   logon();                                       /* logon to Oracle database */
  98.  
  99.   setup();                                          /* prepare sql statement */
  100.  
  101.   insert_data();
  102.  
  103.   logoff();                                        /* logoff Oracle database */
  104.  
  105.   do_exit(OCI_EXIT_SUCCESS);
  106.  
  107. }
  108.  
  109. /* 
  110.  * Function: setup
  111.  *
  112.  * Description: This routine does the necessary setup to execute the SQL
  113.  *              statement. Specifically, it does the open, parse, bind and
  114.  *              define phases as needed.
  115.  *
  116.  */
  117. void setup()
  118. {
  119.  
  120.   if (oopen(&cda, &lda, (text *) 0, -1, -1, (text *) 0, -1))         /* open */
  121.   {
  122.     err_report(&cda);
  123.     do_exit(OCI_EXIT_FAILURE);
  124.   }
  125.  
  126.   if (oparse(&cda, sqlstmt, (sb4) -1, DEFER_PARSE,                  /* parse */
  127.                (ub4) VERSION_7))
  128.   {
  129.     err_report(&cda);
  130.     do_exit(OCI_EXIT_FAILURE);
  131.   }
  132.  
  133.   if (onbtst(&lda))
  134.   {
  135.     printf("connection is still blocking!!!\n");
  136.     do_exit(OCI_EXIT_FAILURE);
  137.   }
  138.  
  139. }
  140.  
  141. /* 
  142.  * Function: insert_data
  143.  *
  144.  * Description: This routine inserts the data into the table
  145.  *
  146.  */
  147. void insert_data()
  148. {
  149.  
  150.    ub1 done = 0;
  151.    static ub1 blocked_cnt = 0;          /* number of times statement blocked */
  152.    while (!done)
  153.    {
  154.      switch(oexec(&cda))
  155.      {
  156.        case BLOCKED:                /* will come through here multiple times */
  157.             blocked_cnt++;
  158.             break;
  159.        case SUCCESS:
  160.         done = 1;
  161.         break;
  162.        default:
  163.         err_report(&cda);
  164.         do_exit(OCI_EXIT_FAILURE);             /* get out of application */
  165.      }
  166.              
  167.    }
  168.  
  169.    printf("\n Execute blocked %ld times\n", blocked_cnt);
  170.  
  171.    if (onbclr(&lda))      /* clear the non-blocking status of the connection */
  172.    {
  173.      err_report((Cda_Def *)&lda);
  174.      do_exit(OCI_EXIT_FAILURE);
  175.    }
  176.  
  177. }
  178.  
  179. /* 
  180.  * Function: err_report
  181.  *
  182.  * Description: This routine prints out the most recent OCI error
  183.  *
  184.  */
  185. void err_report(cursor)
  186. Cda_Def *cursor;
  187. {
  188.     sword n;
  189.     text msg[512];                      /* message buffer to hold error text */
  190.  
  191.     if (cursor->fc > 0)
  192.       printf("\n-- ORACLE error when processing OCI function %s \n\n", 
  193.             oci_func_tab[cursor->fc]);
  194.     else
  195.       printf("\n-- ORACLE error\n");
  196.  
  197.     n = (sword)oerhms(&lda, cursor->rc, msg, (sword) sizeof msg);
  198.     printf("%s\n", msg);
  199.  
  200. }
  201.  
  202. /* 
  203.  * Function: do_exit
  204.  *
  205.  * Description: This routine exits with a status
  206.  *
  207.  */
  208. void do_exit(status)
  209. eword status;
  210. {
  211.  
  212.   if (status == OCI_EXIT_FAILURE)
  213.      printf("\n Exiting with FAILURE status %d\n", status);
  214.   else 
  215.      printf("\n Exiting with SUCCESS status %d\n", status);
  216.      
  217.   exit(status);
  218. }
  219.  
  220. /* 
  221.  * Function: login
  222.  *
  223.  * Description: This routine logs on onto the database as OCITEST/OCITEST
  224.  *
  225.  */
  226. void logon()
  227. {
  228.  
  229.   ub1 done = 0;
  230.   static ub1 logblk_cnt = 0;              /* number of times login blocked */
  231.   while (!done)
  232.   {
  233.      switch (olog(&lda, hda, (text *)"OCITEST", -1, (text *)"OCITEST", -1, 
  234.        (text *)0, -1, OCI_LM_NBL))
  235.      {
  236.  
  237.        case BLOCKED:              /* will come through here multiple times */
  238.             logblk_cnt++;
  239.         break;
  240.        case SUCCESS:
  241.         printf(" Logged on successfully\n");
  242.         done = 1;
  243.         break;
  244.        default:
  245.             err_report((Cda_Def *)&lda);
  246.             do_exit(OCI_EXIT_FAILURE);
  247.      }
  248.  
  249.   }
  250.  
  251.   printf(" Login was blocked %ld times\n", logblk_cnt);
  252.   printf("\n Connected to ORACLE as ocitest\n");
  253.  
  254. }
  255.  
  256. /* 
  257.  * Function: logoff
  258.  *
  259.  * Description: This routine closes out any cursors and logs off the database
  260.  *
  261.  */
  262. void logoff()
  263. {
  264.  
  265.   if (oclose(&cda))                                          /* close cursor */
  266.   {
  267.     printf("Error closing cursor 1.\n");
  268.     do_exit(OCI_EXIT_FAILURE);
  269.   }
  270.  
  271.   if (ologof(&lda))                                  /* log off the database */
  272.   {
  273.     printf("Error on disconnect.\n");
  274.     do_exit(OCI_EXIT_FAILURE);
  275.   }
  276.  
  277. }
  278.